package com.java.database;

import com.java.database.entity.PagingEntity;

import java.util.ArrayList;
import java.util.List;

public  class JunUtil<T> {

    private Integer pageNo = 1;
    private Integer pageSzie = 2;
    private List<T> allData;


    /**
     *  上一页
     */
    public Integer prePageNo(){
        Integer prePageNo = 1;
        if(pageNo - 1 > 1){
            prePageNo = pageNo-1;
        }

        return prePageNo;
    }

    /**
     *  数据总条数
     * @return
     */
    public Integer dataCount(){
        Integer thisDataCount = 0;
       try{
           thisDataCount = allData.size();
       }catch (Exception e){
           throw new RuntimeException("您的数据集异常，无法得到数据总条数");
       }
       return thisDataCount;
    }

    /**
     *  总页数和最后一页
     * @return
     */
    public Integer totalAndLastPage(){
        Integer totalPageCount = 0;
        //   数据总页数
        if(this.dataCount() % pageSzie >0){
            totalPageCount = this.dataCount() / pageSzie +1;
        }else{
            totalPageCount = this.dataCount() / pageSzie;
        }
        return totalPageCount;
    }

    /**
     *  下一页
     * @return
     */
    public Integer nextPageNo(){
        Integer nextPageNo = 0;
        if(pageNo + 1 >= this.totalAndLastPage()){
            nextPageNo = this.totalAndLastPage();
        }else{
            nextPageNo = pageNo+1;
        }

        return nextPageNo;
    }

    /**
     *  分页数据
     * @return
     */
    public List<T> pagingData(){
        List<T> resultList = new ArrayList<T>();

        Integer startIndex = (pageNo -1) * pageSzie;
        Integer endIndex = startIndex + pageSzie;

        for(int i = 0;i<allData.size();i++){
            if(i>= startIndex && i <endIndex){
                resultList.add(allData.get(i));
            }
        }

        return resultList;
    }


    public PagingEntity change(PagingEntity<T> pagingEntity,JunUtil<T> jun,String like){
        pagingEntity.setPageNo(jun.pageNo);
        pagingEntity.setPrePageNo(jun.prePageNo());
        pagingEntity.setNextPageNo(jun.nextPageNo());
        pagingEntity.setList(jun.pagingData());
        pagingEntity.setLike(like);
        pagingEntity.setPageSize(jun.pageSzie);
        pagingEntity.setPageTotal(jun.totalAndLastPage());
        pagingEntity.setDataTotal(jun.dataCount());
        return pagingEntity;
    }


    /**
     *  判断非空
     * @param prama
     * @return
     */
    public boolean ifNull2(Object prama){
         return JunUtil.ifNull(prama);
    }

    /**
     * 转换int类型
     * @param prama
     * @return
     */
    public Integer parseInt2(Object prama){
        return JunUtil.parseInt(prama);
    }

    /**
     *  判断非空
     * @param prama
     * @return
     */
    public static boolean ifNull(Object prama){

        if(prama == null || "".equals(prama)){
            return true;
        }
        return false;
    }

    /**
     *  转换int类型
     * @param prama
     * @return
     */
    public static Integer parseInt(Object prama){
        if(ifNull(prama)){
            throw new RuntimeException("空值不能转换为int类型哦");
        }else{
            Integer result = -1;
            try{
                result = Integer.parseInt(prama.toString());
            }catch(Exception e){
                throw new RuntimeException("非数值类型不能转换为int类型哦");
            }
            return result;
        }
    }


    public Integer getPageNo() {
        return pageNo;
    }

    public void setPageNo(Integer pageNo) {
        this.pageNo = pageNo;
    }

    public Integer getPageSzie() {
        return pageSzie;
    }

    public void setPageSzie(Integer pageSzie) {
        this.pageSzie = pageSzie;
    }

    public List<T> getAllData() {
        return allData;
    }

    public void setAllData(List<T> allData) {
        this.allData = allData;
        this.pagingData();
    }
}
